package com.cnfangmao.masterdata.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.cnfangmao.masterdata.model.Permission;
import com.cnfangmao.masterdata.dao.PermissionDao;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.dto.PermissionDto;
import com.cnfangmao.masterdata.dto.PermissionSearchDto;
import com.cnfangmao.masterdata.service.PermissionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author chenxd
 * @since 2020-04-17
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionDao, Permission> implements PermissionService {

	@Autowired
	private PermissionDao dao;
	
	@Override
	public Permission findByPermissionCode(String permissionCode) {
		Permission permission=null;
		QueryWrapper<Permission> queryWrapper =new QueryWrapper<Permission>();
		queryWrapper.eq("permission_code", permissionCode);
		permission=dao.selectOne(queryWrapper);
		return permission;
	}

	@Override
	public Permission findByPermissionName(String permissionName) {
		Permission permission = null;
		QueryWrapper<Permission> queryWrapper =new QueryWrapper<Permission>();
		queryWrapper.eq("permission_name", permissionName);
		permission=dao.selectOne(queryWrapper);
		return permission;
	}

	
	@Override
	public IPage<Permission> queryListPermission(GeneralSearchDto<PermissionSearchDto> dto) {
		IPage<Permission> page= dao.selectPage(new Page<>(dto.getPageNo(),dto.getPageSize()),new QueryWrapper<Permission>()
				.like("permission_code", dto.getConditions().getPermissionCode())
				.or().like("permission_name", dto.getConditions().getPermissionName())
				);
		return page;
	}

	@Override
	public List<PermissionDto> getAllPermissions() {
		List<PermissionDto> permissionList = new ArrayList<PermissionDto>();
		List<Permission> entityList = dao.findAllPermission();
		for (Permission entity : entityList) {
			PermissionDto permissionDto = new PermissionDto();
			permissionDto.setPermissionId(entity.getPermissionId());
			permissionDto.setPermissionCode(entity.getPermissionCode());
			permissionDto.setPermissionName(entity.getPermissionName());
			permissionDto.setActionPath(entity.getActionPath());
			permissionDto.setRemark(entity.getRemark());
			permissionList.add(permissionDto);

		}
		return permissionList;
	}

}
